package com.zit.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

	private static MessageDigest md = null ;
	
	private MD5(){}
	
	static{
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
	
	public static String md5(String str){
		if(str==null){
			return null ;
		}
		StringBuffer sb = new StringBuffer();
		byte[] datas = md.digest(str.getBytes());
		for (int i = 0; i < datas.length; i++) {
			byte b = datas[i];
			int hi = b>>4&0x0f ;
			int lo = b&0x0f ;
			char hic = (char) (hi<10?('0'+hi):('a'+hi-10));
			char loc = (char) (lo<10?('0'+lo):('a'+lo-10));
			sb.append(hic).append(loc);
		}
		return sb.toString() ;
	}
}
